//
//  PlayersViewController.swift
//  MyNavigationbar
//
//  Created by teleframe on 15/8/18.
//  Copyright (c) 2015年 tf. All rights reserved.
//

import UIKit

class PlayersViewController: UITableViewController {
    
    
    
    var players: [Player] = [ Player(name:"Bill Evans", game:"Tic-Tac-ToeTic-Tac-ToeTic-Tac-ToeTic-Tac-ToeTic-Tac-ToeTic-Tac-ToeTic-Tac-Toe", rating: 4),
        Player(name: "Oscar Peterson", game: "Spin the Bottle", rating: 5),
        Player(name: "Dave Brubeck", game: "Texas Hold 'em Poker", rating: 2),
        Player(name: "Oscar Peterson", game: "Spin the Bottle", rating: 5),
        Player(name: "Dave Brubeck", game: "Texas Hold 'em Poker", rating: 2) ,
        Player(name: "Oscar Peterson", game: "Spin the Bottle", rating: 5),
        Player(name: "Dave Brubeck", game: "Texas Hold 'em Poker", rating: 2) ,
        Player(name: "Oscar Peterson", game: "Spin the Bottle", rating: 5),
        Player(name: "Dave Brubeck", game: "Texas Hold 'em Poker", rating: 2) ,
        Player(name: "Oscar Peterson", game: "Spin the Bottle", rating: 5),
        Player(name: "Dave Brubeck", game: "Texas Hold 'em Poker", rating: 2) ,
        Player(name: "Oscar Peterson", game: "Spin the Bottle", rating: 5),
        Player(name: "Dave Brubeck", game: "Texas Hold 'em Poker", rating: 2) ,
        Player(name: "Oscar Peterson", game: "Spin the Bottle", rating: 5),
        Player(name: "Dave Brubeck", game: "Texas Hold 'em Poker", rating: 2) ,
        Player(name: "Oscar Peterson", game: "Spin the Bottle", rating: 5),
        Player(name: "Dave Brubeck", game: "Texas Hold 'em Poker", rating: 2) ,
        Player(name: "Oscar Peterson", game: "Spin the Bottle", rating: 5),
        Player(name: "Dave Brubeck", game: "Texas Hold 'em Poker", rating: 2) ,
        Player(name: "Oscar Peterson", game: "Spin the Bottle", rating: 5),
        Player(name: "Dave Brubeck", game: "Texas Hold 'em Poker", rating: 2) ,
        Player(name: "Oscar Peterson", game: "Spin the Bottle", rating: 5),
        Player(name: "Dave Brubeck", game: "Texas Hold 'em Poker", rating: 2)  ]
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.estimatedRowHeight = 84.0
        self.tableView.rowHeight = UITableViewAutomaticDimension;
    }
    override func viewWillAppear(animated: Bool) {
//
        self.navigationController?.navigationBar.barTintColor = UIColor.applicationMainBarColor()
        self.navigationController?.navigationBar.translucent = false
    }
    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return players.count
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
        -> UITableViewCell {
            let cell : PlayerCell = tableView.dequeueReusableCellWithIdentifier("PlayerCell", forIndexPath: indexPath)
                as! PlayerCell
            
            let player = players[indexPath.row] as Player
            cell.name.text = player.name
            cell.game.text = player.game
            cell.rant.image = self.imageForRating(player.rating)
            return cell
    }
//    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
//    
//       return UITableViewAutomaticDimension
//    
//    }
//    
    
    func imageForRating(rating:Int) -> UIImage? {
        switch rating {
        case 1:
            return UIImage(named: "1StarSmall")
        case 2:
            return UIImage(named: "2StarsSmall")
        case 3:
            return UIImage(named: "3StarsSmall")
        case 4:
            return UIImage(named: "4StarsSmall")
        case 5:
            return UIImage(named: "5StarsSmall")
        default:
            return nil
        }
    }
    
    
}
